import { ApiActionType, ArticleDetail ,Comment} from "@/types/data";
import { ArticleDetailAction, RootThunkAction } from "@/types/store";
import request from "@/utils/request";
import { Dispatch } from "react";


export  const getArticleDetail=(id:string):RootThunkAction=>{
 return async(dispatch:Dispatch<ArticleDetailAction>)=>{
    const res = await request.get<ApiActionType<ArticleDetail>>(`/articles/${id}`)
    // console.log('文章详情是', res.data.data)
    dispatch({
        type:'articleDetail/setArticleInfo',
        payload:res.data.data
    })
 }
}

// 获取文章的评论
export function getCommentList(id: string,offset?:string): RootThunkAction {
    return async (dispatch) => {
      const res = await request.get<ApiActionType<Comment>>('/comments', {
        params: {
          type: 'a',
          source: id,
          offset
        }
      })
      console.log(res,'1123132') // 输出结果，查看返回值的类型
      dispatch({
        type: 'articleDetail/saveComment',
        payload: res.data.data,
      })
    }
  }